<?php

class C_Dispatcher 
{
	private $_request = null;
	private $_response = null;
	private $_module = '';
	private $_action = '';
	private $_method = '';
	private $_error = 'error';
	private $_formatModule = '';
	private $_formatAction = '';
	private $_formatMethod = '';
	public function __construct($request, $response)
	{
		$this->_request = $request;
		$this->_response = $response;
		$this->_response->getDispatcher($this);
		$this->_module = $this->_request->getModule();
		$this->_action = $this->_request->getAction();
		$this->_method = $this->_request->getMethod();
	}

  	public function dispatch($while = false, $module = '',  $action = '',  $method = '')
    {
    
        if(!$while)
        {
			$this->_formatModule = format_module($this->_module);
			$this->_formatAction = format_action($this->_action);
			$this->_formatMethod = format_method($this->_method);
        }
        else 
        {
        	$this->_formatModule = format_module($module);
			$this->_formatAction = format_action($action);
			$this->_formatMethod = format_method($method);
        }
  		
        $loadFile = load_action_path($this->_formatModule, $this->_formatAction);
        
        if(!file_exists($loadFile))
		{
			$loadFile = $this->loadErrorFile();
		}
		include_once $loadFile;
	
		if(!class_exists($this->_formatModule.'_'.$this->_formatAction))
		{
				
			if($this->_formatAction == format_action($this->_error))
			{
				 throw new Exception('错误类没找到');
			}
			else 
			{
				$this->_formatAction = format_action($this->_error);
			}
		}
		$actionclass = $this->_formatModule.'_'.$this->_formatAction;
		
		$actionClassHandle = new $actionclass($this->_request, $this->_response);
		
		if(!method_exists($actionClassHandle, $this->_formatMethod))
		{
			
			if($this->_formatMethod == format_method($this->_error))
			{
				 throw new Exception('错误方法没找到');
			}
			else
			{
				$this->_formatMethod = format_method($this->_error);
			}
		}
		
		$this->_request->setformatModule($this->_formatModule);
		$this->_request->setformatAction($this->_formatAction);
		$this->_request->setformatMethod($this->_formatMethod);
		$actionClassHandle->dispatch($this->_formatMethod);
		
    }
    public function startAction($module,  $action,  $method)
    {
    	$this->dispatch(true, $module,  $action,  $method);
    }
    public function dispachError()
    {
     	$module = format_module($this->_error);
		$action = format_action($this->_error);
		$method = format_method($this->_error);
    	$this->dispatch(true, $module,  $action,  $method);
    }
    public function loadErrorFile()
    {
    	$this->_formatModule = format_module($this->_error);
		$this->_formatAction = format_action($this->_error);
		$this->_formatMethod = format_method($this->_error);
		$loadFile = load_action_path($this->_formatModule, $this->_formatAction);
		if(!file_exists($loadFile))
		{
			 throw new Exception('错误文件没找到');
		}
		else
		{
			return  $loadFile;
		}
    }

}

?>